/**
 * @description:
 * @author: asd
 * @time: 2022/10/17 21:16
 */
public class GuardedObject <V>{
    private boolean isOk;
    private final Object lock = new Object();
    private V v;
    public void set(V v){
        synchronized (lock){
            this.v = v;
            isOk = true;
            lock.notifyAll();
        }
    }

    public V get(long millis){
        long  base = System.currentTimeMillis();
        long  now  = 0;
        synchronized (lock){
            while (!isOk){
                if(now >= millis){
                    System.out.println("超时");
                    break;
                }
                try {
                    lock.wait(millis - now);
                } catch (InterruptedException e) {
                    now = System.currentTimeMillis() - base;
                    System.out.println("被打断了");
                    throw new RuntimeException(e);
                }
            }
        }
        return v;
    }
}
